﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MaLu.Data;
using MaLu.Common;
using MaLu.BusiLogic;
using System.Text;
using MaLu.Framework.Common;

namespace MaluJg.web.PovertyIn
{
    public partial class UOther : PUserControl
    {
        public void SaveData()
        {
            float f = 0.0f;
            float temp = 0.0f;
            float.TryParse(tbTown.Text.Trim(), out temp);
            f += temp;
          

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("declare @id int; set @id={0}; select @id=id from SOthers where id=@id;", RecordID);
            sb.AppendLine("if @@rowcount =0 begin");
            sb.AppendFormat(@"INSERT INTO [SOthers]
           ([Name],[Address],[Male],[Female],[Reason],[Town],
           [PartyCount],[BelongTo],[Year],[Month],[Money])
     VALUES
           ({0},{1},{2},{3},'{4}',{5},{6},'{7}',{8},{9},'{10}'); end else begin

UPDATE [SOthers]
   SET [Name] ={0}     ,[Address] = {1}
      ,[Male] = {2}     ,[Female] =  {3}
      ,[Reason] =  '{4}'      ,[Town] = {5}
      ,[PartyCount] =  {6}     ,[BelongTo] = '{7}'
      ,[Year] =  {8},[Month] =  {9},[Money] =  '{10}' 
 WHERE id=@id ; end", Util.ReturnSqlValue(tbName.Text),  Util.ReturnSqlValue(tbAdd.Text), Util.ReturnSqlValue(tbMail.Text),
                    Util.ReturnSqlValue(tbFeMail.Text), "", Util.ReturnSqlValue(tbTown.Text), 
                    Util.ReturnSqlValue(tbPartyCount.Text), ddlVillage.SelectedItem.Text,
                    Util.ReturnSqlValue(tbYear.Text), ddlMonth.SelectedValue, f);

            SqlHelper.Instance.ExecuteNonQuery(Provider, sb.ToString());

        }
        public void LoadData()
        {
            ddlVillage.DataSource = BLPoverty.GetFType(Provider);
            ddlVillage.DataTextField = "name";
            ddlVillage.DataValueField = "id";
            ddlVillage.DataBind();
            ddlMonth.DataSource = Util.GetMonth();
            ddlMonth.DataTextField = "text";
            ddlMonth.DataValueField = "value";
            ddlMonth.DataBind();
            DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, @"SELECT *  FROM [SOthers] where id=" + RecordID);
            if (dt.Rows.Count > 0)
            {
                LinkButton lbtnSave = Page.Master.FindControl("ToolBarContentPlaceHolder").FindControl("lbtnSave") as LinkButton;

                lbtnSave.Text = "<span ><img src=\"../Images/Icon/Action/save.gif\" alt=\"保存\" /></span><span>保存</span>";
                Page.Title = "扶贫帮困资金编辑";
                DataRow dr = dt.Rows[0];
                tbYear.Text = dr["Year"].ToString();
                tbName.Text = dr["Name"].ToString();
                tbAdd.Text = dr["address"].ToString();
               
                tbTown.Text = dr["town"].ToString();
                tbFeMail.Text = dr["Female"].ToString();
                tbMail.Text = dr["male"].ToString();
                tbPartyCount.Text = dr["PartyCount"].ToString();


                ListItem li = ddlVillage.Items.FindByText(dr["BelongTo"].ToString());
                if (li != null)
                    ddlVillage.SelectedValue = li.Value;
                li = ddlMonth.Items.FindByValue(dr["month"].ToString());
                if (li != null)
                    ddlMonth.SelectedValue = li.Value;

              
            }
        }
    }
}